import request from './request'
import type { Product } from './product'

// 购物车项
export interface CartItem {
  id: number
  userId: number
  productId: number
  product?: Product
  quantity: number
  selected: boolean
  createdAt: string
  updatedAt: string
}

// 添加到购物车请求
export interface AddToCartRequest {
  productId: number
  skuId?: number
  quantity: number
}

// 更新购物车项请求
export interface UpdateCartItemRequest {
  quantity?: number
  selected?: boolean
}

export const cartApi = {
  // 获取购物车列表
  getCartItems: () => {
    return request.get<CartItem[]>('/api/cart')
  },

  // 添加商品到购物车
  addToCart: (data: AddToCartRequest) => {
    return request.post<CartItem>('/api/cart', data)
  },

  // 更新购物车项
  updateCartItem: (id: number, data: UpdateCartItemRequest) => {
    return request.put<CartItem>(`/api/cart/${id}`, data)
  },

  // 删除购物车项
  removeCartItem: (id: number) => {
    return request.delete(`/api/cart/${id}`)
  },

  // 清空购物车
  clearCart: () => {
    return request.delete('/api/cart')
  },

  // 批量选择/取消选择
  batchSelect: (ids: number[], selected: boolean) => {
    return request.put('/api/cart/batch-select', { ids, selected })
  },

  // 全选/取消全选
  selectAll: (selected: boolean) => {
    return request.put('/api/cart/select-all', { selected })
  }
}